package com.tencent.backstage.controller;

import com.tencent.backstage.configuration.DomainName;
import com.tencent.backstage.dao.NoticeDao;
import com.tencent.backstage.dao.SuggestDao;
import com.tencent.backstage.dao.mapper.SuggestMapper;
import com.tencent.backstage.dao.mongoDao.UserRecordDao;
import com.tencent.backstage.entity.Notice;
import com.tencent.backstage.utils.ResultVOUtil;

import com.tencent.backstage.vo.NoticeVO;
import com.tencent.backstage.vo.ResultVO;
import com.tencent.backstage.vo.SuggestPageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户登陆注册页面
 * Created by 楠尼玛
 * 2018-03-31 16:20
 */
@RestController
@RequestMapping("/fire")
@Slf4j
public class UserController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private DomainName domainName;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private SuggestMapper suggestMapper;
    @Autowired
    private NoticeDao noticeDao;
    @PostMapping("/getTypeRanking")
    public ResultVO getTypeRanking(){
        return  ResultVOUtil.success(userRecordDao.getTypeRanking());
    }
    @PostMapping("/getFirstRecordLimit")
    public ResultVO getFirstRecordLimit(@RequestParam("limit") Integer limit,@RequestParam("skip") Integer skip){
        return  ResultVOUtil.success(userRecordDao.getFirstRecordLimit(limit,skip));
    }
    @PostMapping("/getPN")
    public ResultVO getPN(){
        return ResultVOUtil.success(userRecordDao.getPN());
    }
    @PostMapping("/findById")
    public ResultVO findById(@RequestParam("id") String id){
        return ResultVOUtil.success(userRecordDao.findById(id));
    }
    @PostMapping("/reloadPicture")
    public ResultVO reloadPicture(@RequestParam("limit") Integer limit,@RequestParam("skip") Integer skip){
        return ResultVOUtil.success(userRecordDao.reloadPicture(limit,skip));
    }
    @PostMapping("/findByOpenid")
    public ResultVO findByOpenid(@RequestParam("openid") String openid){
        return ResultVOUtil.success(userRecordDao.findByOpenid(openid));
    }
    @PostMapping("/getSuggest")
    public ResultVO getSuggest(@RequestParam(value = "skip") Integer skip,
                               @RequestParam(value = "limit") Integer limit){
        List<SuggestPageVO> result =suggestMapper.getSuggest(skip,limit);
//        if(result.getTotalPage()>page){
//            result.setHasNextPage(true);
//        }else{
//            result.setHasNextPage(false);
//        }
        return ResultVOUtil.success(result);
    }


    //公告类
    @PostMapping("/getNotice")
    public ResultVO getNotice(){
        List<Notice> notices =noticeDao.findAll();
        return ResultVOUtil.success(notices);
    }
    @PostMapping("/deleteNotice")
    public ResultVO deleteNotice(@RequestParam("id")Integer id){
        noticeDao.deleteById(id);
        return ResultVOUtil.success("deleteSucceess");
    }
  @PostMapping("/findByIdNotice")
    public ResultVO findByIdNotice(@RequestParam("id")Integer id){
        Notice notice =  noticeDao.findById(id);
        return ResultVOUtil.success(notice);
    }
    @PostMapping("/saveNotice")
    public ResultVO saveNotice(NoticeVO noticeVO){
        Notice notices = new Notice();
        BeanUtils.copyProperties(noticeVO,notices);
        notices.setCreateTime(new Date());
        Notice notice =  noticeDao.save(notices);
        return ResultVOUtil.success(notice);
    }

}

